# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        博智科技  
# Name:         demo1421
# Description:
# Author:       yzl
# Date:         2019-02-10
#-------------------------------------------------------------------------------



import json


# data = {
#     'name':'bill',
#     'age':20,
#     'company':'微软'
# }
#
# # 将字典转换成字符串
# jsonstr = json.dumps(data)
# print(type(jsonstr))
# print(jsonstr)
# print('-----------------------')
#
# # 将字符串转换成字典
# data1 = json.loads(jsonstr)
# print(type(data1))
# print(data1['name'])
# print(data1)
#
# print('-----------------------')
# # 注意 key-value 使用双引号
# try:
#     json_obj = """{
#         "name":"zhangsan",
#         "age":"40",
#         "addr":{
#             "province":"湖南",
#             "city":"株洲市"
#         }
#     }"""
#
#     json_dict = json.loads(json_obj)
#     print(json_dict)
# except Exception as e:
#     print(e)


s = '''
{
    "name":"bill",
    "age":20,
    "company":"微软"
}
'''
s1 = '''
{
    'name':'bill',
    'age':20,
    'company':'微软'
}
'''
# 使用 eval 将字符串转换成字典
# data = eval(s1)
# print(data['age'])
#
# data1 = json.loads(s)
# print(data['name'])

# 读取 json格式的文件,转换成字典
f = open('files/products.json','r',encoding='utf-8')
jsonstr = f.read()
json1 = eval(jsonstr)
print(json1)

json2 = json.loads(jsonstr)
print(json2)

